package pack.jcus_notifier;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends Activity {
	Button b;
	EditText et,pass;
	TextView tv;
	HttpPost httppost;
	StringBuffer buffer;
	HttpResponse response;
	HttpClient httpclient;
	List<NameValuePair> nameValuePairs;
	ProgressDialog dialog = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        b = (Button)findViewById(R.id.Button01);  
        et = (EditText)findViewById(R.id.username);
        pass= (EditText)findViewById(R.id.password);
        tv = (TextView)findViewById(R.id.tv);
        
        b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog = ProgressDialog.show(Home.this, "", "Validating user...", true);
				new Thread(new Runnable() {
				public void run() 
				{
					jclogin();					      
				}
				}).start();				
			}
		});
    }
	
	void jclogin(){
		try{			
			 
			httpclient=new DefaultHttpClient();
			httppost= new HttpPost("http://10.0.2.2/my_folder_inside_htdocs/check.php"); // make sure the url is correct.
			nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
			nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
			response=httpclient.execute(httppost);
			
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			final String response = httpclient.execute(httppost, responseHandler);
			System.out.println("Response : " + response); 
			runOnUiThread(new Runnable() 
			{
			    public void run() 
			    {
			    	tv.setText("Status : " + response);
					dialog.dismiss();
			    }
			});
			
			if(response.equalsIgnoreCase("User Found"))
			{
				runOnUiThread(new Runnable() 
				{
				    public void run() 
				    {
				    	Toast.makeText(Home.this,"Login_Success", Toast.LENGTH_LONG).show();
				    }
				});
				
				startActivity(new Intent(Home.this, UserPage.class));
			}
			else
			{
				showAlert();				
			}
			
		}catch(Exception e)
		{
			dialog.dismiss();
			System.out.println("Exception : " + e.getMessage());
		}
	}
	public void showAlert()
	{
		Home.this.runOnUiThread(new Runnable() 
		{
		    public void run() 
		    {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(Home.this);
		    	builder.setTitle("Invalid Login");
		    	builder.setMessage("User not Found")  
		    	       .setCancelable(false)
		    	       .setPositiveButton("OK", new DialogInterface.OnClickListener() 
		    	       {
		    	           public void onClick(DialogInterface dialog, int id) 
		    	           {
		    	           }
		    	       });		    	       
		    	AlertDialog alert = builder.create();
		    	alert.show();		    	
		    }
		});
	}
}